package com.xw.unification.helper;

/**
 * @author qiwei
 */
public class NullableObj<T> {

    private T obj;

    NullableObj(T obj) {
        this.obj = obj;
    }

    public NullableObj<T> or(T obj) {
        if (this.obj == null) {
            this.obj = obj;
        }
        return this;
    }

    public boolean isPresent() {
        return obj != null;
    }

    public T end() {
        return obj;
    }

    public T endWith(T obj) {
        or(obj);
        return obj;
    }

    public T escape(RuntimeException exception) {
        if (obj == null) {
            throw exception != null ? exception : new RuntimeException("Object is null.");
        }
        return obj;
    }

}
